package app.server.project.main.service.db.dao.cfg;

import app.server.project.core.constants.Def;
import app.server.project.main.service.constants.GlobalCfgKey;
import app.server.project.main.service.db.entity.cfg.GlobalCfgEntity;
import app.server.project.main.service.db.mapper.cfg.GlobalCfgMapper;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.PostConstruct;
import lombok.Data;
import org.springframework.stereotype.Repository;

import java.util.List;

@Data
@Repository
public class GlobalCfgDao extends ServiceImpl<GlobalCfgMapper, GlobalCfgEntity> {

    private List<GlobalCfgEntity> globalCfgEntityList;

    private void flush() {
        this.globalCfgEntityList = this.list();
    }

    @PostConstruct
    public void init() {
        this.flush();
    }

    public GlobalCfgEntity getOneByKey(String key) {
        return this.globalCfgEntityList.stream().filter(v -> v.getId().equals(key)).findFirst().get();
    }

    public String getStr(String key) {
        var ret = this.globalCfgEntityList.stream().filter(v -> v.getId().equals(key)).findAny();
        if (ret.isEmpty()) return StrUtil.EMPTY;
        return ret.get().getVal();
    }

    public boolean has(String key) {
        return this.globalCfgEntityList.stream().anyMatch(v -> v.getId().equals(key));
    }

    public boolean getDebug() {
        var cfg = this.globalCfgEntityList.stream().filter(v -> v.getId().equals(GlobalCfgKey.Server_Debug)).findFirst();
        if (cfg == null) return false;
        return cfg.get().getLong() >= Def.True;
    }


}
